package com.Fate.dao;

import com.Fate.common.PageInfo;
import com.Fate.pojo.User;

import java.util.List;
import java.util.Map;

public class UserDao extends BaseDao{
    /**
     * 根据用户名和密码查询用户
     */
    public User selectByNameAndPassword(String username,String password){
        String sql = "select id ,username,password,realname,role,deleted,img from users where username = ? and password = ? ";
        return super.selectOne(sql,User.class,username,password);
    }

    public PageInfo<User> selectPage(Map<String,String> params,Integer pageNo,Integer pageSize){
        String sql = "select id,username,password,realname,role,deleted,img,create_time as createTime,deleted_time as deletedTime from users where 1=1 ";
        //拼接动态sql
        if(params.containsKey("username")){
            sql = sql + " and username like '%"+params.get("username")+"%'";
        }
        if (params.containsKey("realname")){
            sql = sql + " and realname like '%"+params.get("realname")+"%'";
        }
        if (params.containsKey("role")){
            sql = sql + " and role ="+params.get("role")+" ";
        }
        if (params.containsKey("deleted")){
            sql = sql+" and deleted ="+params.get("deleted")+" ";
        }
        return super.selectPage(sql,User.class,pageNo,pageSize);
    }

    /**
     * 根据id 修改密码
     * @param id
     * @param password
     */
    public void updatePassword(String id, String password) {
        String sql= " update users set password = ? where id=? ";
        super.update(sql,password,id);
    }

    /**
     * 根据id 修改离职
     * @param id
     * @param userDeleteEd
     */
    public void del(String id, Integer userDeleteEd) {
        String sql= " update users set deleted = ? where id=? ";
        super.update(sql,userDeleteEd,id);
    }



    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    public User selectByUsername(String username) {
        String sql="select id,username from user where username =?";

        return super.selectOne(sql,User.class,username);
    }

    /**
     * 插入新用户
     * @param username
     * @param password
     * @param realname
     * @param role
     * @param deleted
     */
    public void insert(String username, String password, String realname, Integer role, Integer deleted) {
        String sql="INSERT INTO `users` ( `username`, `password`, `realname`, `role`, `deleted`) VALUES(?,?,?,?,?) ;";
        super.update(sql,username,password,realname,role,deleted);
    }

    /**
     * 根据状态和角色查询用户
     * @param deleted
     * @param role
     * @return
     */
    public List<User> selectList(Integer deleted, Integer role) {
        String sql = "select id,realname from users where deleted =? and role =?";
        return  super.selectList(sql,User.class,deleted,role);
    }





















}
